﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using IIS_Local4.Models;

namespace IIS_Local4.Controllers
{
    /*
    이 컨트롤러에 대한 경로를 추가하려면 WebApiConfig 클래스를 추가로 변경해야 할 수 있습니다. 가능한 경우 WebApiConfig 클래스의 Register 메서드에 이 문을 병합합니다. OData URL은 대/소문자를 구분합니다.

    
    
    */
    public class productplan_putController : ODataController
    {
        private Model1 db = new Model1();

        // GET: odata/productplan_put
        [EnableQuery]
        public IQueryable<productplan_put> Getproductplan_put()
        {
            return db.productplan_put;
        }

        // GET: odata/productplan_put(5)
        [EnableQuery]
        public SingleResult<productplan_put> Getproductplan_put([FromODataUri] string key)
        {
            return SingleResult.Create(db.productplan_put.Where(productplan_put => productplan_put.productplan_code == key));
        }

        // PUT: odata/productplan_put(5)
        public async Task<IHttpActionResult> Put([FromODataUri] string key, Delta<productplan_put> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            productplan_put productplan_put = await db.productplan_put.FindAsync(key);
            if (productplan_put == null)
            {
                return NotFound();
            }

            patch.Put(productplan_put);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!productplan_putExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(productplan_put);
        }

        // POST: odata/productplan_put
        public async Task<IHttpActionResult> Post(productplan_put productplan_put)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.productplan_put.Add(productplan_put);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (productplan_putExists(productplan_put.productplan_code))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return Created(productplan_put);
        }

        // PATCH: odata/productplan_put(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] string key, Delta<productplan_put> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            productplan_put productplan_put = await db.productplan_put.FindAsync(key);
            if (productplan_put == null)
            {
                return NotFound();
            }

            patch.Patch(productplan_put);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!productplan_putExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(productplan_put);
        }

        // DELETE: odata/productplan_put(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] string key)
        {
            productplan_put productplan_put = await db.productplan_put.FindAsync(key);
            if (productplan_put == null)
            {
                return NotFound();
            }

            db.productplan_put.Remove(productplan_put);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        // GET: odata/productplan_put(5)/Location_table
        [EnableQuery]
        public SingleResult<Location_table> GetLocation_table([FromODataUri] string key)
        {
            return SingleResult.Create(db.productplan_put.Where(m => m.productplan_code == key).Select(m => m.Location_table));
        }

        // GET: odata/productplan_put(5)/product
        [EnableQuery]
        public SingleResult<product> Getproduct([FromODataUri] string key)
        {
            return SingleResult.Create(db.productplan_put.Where(m => m.productplan_code == key).Select(m => m.product));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool productplan_putExists(string key)
        {
            return db.productplan_put.Count(e => e.productplan_code == key) > 0;
        }
    }
}
